﻿using System;
using System.Diagnostics;

namespace Lenic.DI
{
    /// <summary>
    /// DI 容器注册异常类
    /// </summary>
    [DebuggerStepThrough]
    public class RegisterException : Exception
    {
        /// <summary>
        /// 初始化新建一个 <see cref="RegisterException" /> 类的实例对象
        /// </summary>
        /// <param name="serviceType">注册项的目标类型。</param>
        /// <param name="name">注册项的名称。</param>
        public RegisterException(Type serviceType, string name)
            : base(string.Format("已经注册类型为[{0}]名称为[{1}]的项", serviceType.FullName, name ?? "null"))
        {
        }

        /// <summary>
        /// 初始化新建一个 <see cref="RegisterException"/> 类的实例对象
        /// </summary>
        /// <param name="msg">异常文本</param>
        public RegisterException(string msg)
            : base(msg)
        {
        }
    }

    /// <summary>
    /// DI 容器解析异常类
    /// </summary>
    [DebuggerStepThrough]
    public class ResolveException : Exception
    {
        /// <summary>
        /// 初始化新建一个 <see cref="ResolveException" /> 类的实例对象
        /// </summary>
        /// <param name="serviceType">发生错误的目标类型。</param>
        /// <param name="name">待解析的名称。</param>
        public ResolveException(Type serviceType, string name)
            : base(string.Format("没有发现类型为[{0}]名称为[{1}]的项", serviceType.FullName, name ?? "null"))
        {
        }
    }

    /// <summary>
    /// Exception infomation class
    /// </summary>
    [DebuggerStepThrough]
    public sealed class ExceptionInfo
    {
        /// <summary>
        /// Gets or sets the exception.
        /// </summary>
        public Exception Exception { get; private set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="ExceptionInfo"/> is rethrow.
        /// <c>true</c> if rethrow; otherwise, <c>false</c>.
        /// </summary>
        public bool Rethrow { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExceptionInfo"/> class.
        /// </summary>
        /// <param name="e">The exception, default is <c>null</c>.</param>
        public ExceptionInfo(Exception e = null)
        {
            Exception = e;
            Rethrow = e != null ? true : false;
        }
    }
}